package com.zhuchaooasystem.presenter.home.message;

import android.app.Activity;

import com.zhuchaooasystem.api.HttpFactory;
import com.zhuchaooasystem.api.HttpUtil;
import com.zhuchaooasystem.api.RxResultHelper;
import com.zhuchaooasystem.base.BasePresenter;
import com.zhuchaooasystem.base.SchedulersCompat;
import com.zhuchaooasystem.entity.ServiceMessageEntity;
import com.zhuchaooasystem.entity.TagServiceMessageEntity;
import com.zhuchaooasystem.entity.UserInfo;

import java.util.Map;

import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by zhangyajiang on 2016/12/22.
 */

public class ServiceMessagePresenter extends BasePresenter<ServiceMessageContract.View> implements ServiceMessageContract.Presenter {

    public ServiceMessagePresenter(Activity activity, ServiceMessageContract.View view) {
        super(activity, view);
    }

    @Override
    public void getMessage(final int index) {
        //----
        final int userId = UserInfo.getInstance().getMemberId();
        final Map<String, String> map = HttpUtil.getHttpMap(mActivity);
        map.put("ID", userId + "");
        map.put("Level", "0");
        map.put("PageIndex", index + "");

        //----
        Subscription subscription = HttpFactory.getHttpApi().searchUserGetNotifyMessage(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult()).subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        if (null != o) {
                            ServiceMessageEntity entity = (ServiceMessageEntity) o;
                            if (null != entity.getResult().getSuccessful()) {
                                if (index == 1) {
                                    mView.refreshCount(entity.getResult().getSuccessful().getRowCount());
                                    mView.refreshData(entity.getResult().getSuccessful().getResopnseMeg());
                                } else {
                                    mView.loadMoreData(entity.getResult().getSuccessful().getResopnseMeg());
                                }
                            } else if (null != entity.getResult().getError()) {
                                handleError(entity.getResult().getError());
                            }

                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                    }
                });
        addSubscrebe(subscription);
    }

    @Override
    public void changeReadState(int ProjectId) {
        //----
        final Map<String, String> map = HttpUtil.getHttpMap(mActivity);
        map.put("ProjectId", ProjectId + "");
        //----
        Subscription subscription = HttpFactory.getHttpApi().tagPushMessageToRecRead(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult()).subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        if (null != o) {
                            TagServiceMessageEntity entity = (TagServiceMessageEntity) o;
                            if (null != entity.getResult().getSuccessful()) {
                                mView.changeStateSuccuss();
                            } else if (null != entity.getResult().getError()) {
                                handleError(entity.getResult().getError());
                            }
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                    }
                });
        addSubscrebe(subscription);
    }
}
